<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" :model="listQuery" class="filter-container">
      <!-- <el-form-item prop="meetingId">
        <el-input v-model="listQuery.meetingId" size="small" placeholder="Meeting ID" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="userId">
        <el-input v-model="listQuery.userId" size="small" placeholder="User ID" @keyup.enter.native="handleFilter" />
      </el-form-item>

      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >筛选</el-button>
        <el-button
          size="small"
          @click="resetFilter"
        >重置</el-button>
      </el-form-item> -->

      <el-form-item>
        <el-upload
          :action="uploadUrl"
          :multiple="false"
          :limit="1"
          name="files"
          :headers="{token}"
          :data="{liveBroadcastId}"
          :on-success="handleUploadSucc"
          :show-file-list="false"
          accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
        >
          <el-button
            size="small"
            type="success"
            icon="el-icon-upload2"
          >导入记录</el-button>
          <div slot="tip" class="el-upload__tip">仅支持上传csv/xls/xlsx文件</div>
        </el-upload>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column label="Meeting ID" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.meeting_id }}
        </template>
      </el-table-column>
      <el-table-column label="User ID" align="center">
        <template slot-scope="scope">
          {{ scope.row.user_id }}
        </template>
      </el-table-column>
      <el-table-column label="昵称">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column label="手机号码" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.mobile }}
        </template>
      </el-table-column>
      <el-table-column label="邮箱">
        <template slot-scope="scope">
          {{ scope.row.email }}
        </template>
      </el-table-column>
      <el-table-column label="进入时间" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.join_time_str }}
        </template>
      </el-table-column>
      <el-table-column label="离开时间" width="160" align="center">
        <template slot-scope="scope">
          {{ scope.row.leave_time_str }}
        </template>
      </el-table-column>
      <el-table-column label="观看总时长" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.on_line_time }}
        </template>
      </el-table-column>
      <el-table-column label="IP">
        <template slot-scope="scope">
          {{ scope.row.login_ip }}
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="fetchData" />
  </div>
</template>

<script>
import {
  liveBroadcastShow,
  getLiveBroadcastsRecords
} from '@/api/liveBroadcasts'
import Pagination from '@/components/Pagination'
import { mapGetters } from 'vuex'

export default {
  name: 'LiveBroadcastRecords',
  components: {
    Pagination
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        // meetingId: undefined,
        // userId: undefined,
        page: 1,
        size: 30
      },
      uploadUrl: '',
      liveBroadcastId: ''
    }
  },
  computed: {
    ...mapGetters([
      'token'
    ])
  },
  // watch: {
  //   $route: {
  //     handler(route) {
  //       this.listQuery.id = route.params && route.params.id
  //       if (!this._.isNil(route.query.page) &&
  //         !this._.isEmpty(route.query.page)
  //       ) {
  //         this.listQuery.page = parseInt(route.query.page)
  //       } else {
  //         this.listQuery.page = 1
  //       }
  //     },
  //     immediate: true
  //   }
  // },
  created() {
    this.liveBroadcastId = this.$route.params.id
    this.listQuery.liveBroadcastId = this.$route.params.id
    if (!this._.isNil(this.$route.query.page) &&
      !this._.isEmpty(this.$route.query.page)
    ) {
      this.listQuery.page = parseInt(this.$route.query.page)
    } else {
      this.listQuery.page = 1
    }
    this.fetchData()
    this.fetchLiveShow()
  },
  methods: {
    fetchLiveShow() {
      liveBroadcastShow({
        id: this.liveBroadcastId
      }).then(response => {
        const { data } = response
        // console.log(data)
        if (data.platformId === 1) {
          // 上传微吼直播记录
          this.uploadUrl = process.env.VUE_APP_BASE_API + '/csvService/uploadOtherCSV'
        } else if (data.platformId === 2) {
          // 上传会畅直播记录
          this.uploadUrl = process.env.VUE_APP_BASE_API + '/csvService/uploadCSV'
        }
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData() {
      this.listLoading = true
      const query = {
        ...this.$route.query,
        page: this.listQuery.page
      }
      this.$router.push({
        query
      }).catch(err => {
        console.log(err)
      })
      getLiveBroadcastsRecords(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.list = []
      this.fetchData()
    },
    resetFilter() {
      this.$refs.filterForm.resetFields()
      this.$router.push({
        query: {
          page: 1
        }
      }).catch(err => {
        console.log(err)
      })
      this.handleFilter()
    },
    handleUploadSucc(res) {
      // console.log(res)
      this.fetchData()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-upload__tip {
  line-height: normal;
}
</style>
